package com.k2data.k2app.service;


import com.k2data.k2app.domain.deviceRest.SysLog;
import com.k2data.k2app.rest.SystemClient;
import com.k2data.k2app.util.DateFormatterUtil;
import com.k2data.k2app.util.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从device模块取回来的数据
 * Created by clb on 17-3-30.
 */
@Log4j2
@Service
public class SystemService {

    private final RestTemplate restTemplate;
    @Autowired
    SystemClient systemClient;

    private static final String modelName = "SYS";
    public static final String LOG_ERROR = "error";
    public static final String LOG_NORMAL = "normal";
    public static final String LOG_WARING = "warning";
    @Autowired
    public SystemService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 插入日志
     * @param className   类名
     * @param methodName  方法
     * @param message     消息
     * @param detail      详情
     * @param type        类型
     * @return
     * @throws Exception
     */
    public Map insertLog(String className,String methodName,String message,String detail,String type) throws Exception{
        SysLog sysLog = new SysLog();
        sysLog.setClassName(className);
        sysLog.setDetail(detail);
        sysLog.setMethod(methodName);
        sysLog.setMessage(message);
        sysLog.setType(type);
        sysLog.setCreateDate(new Date());
        Map result = systemClient.insertLog(sysLog).getResult();
        return result;
    }

    public Map getUsefulMailSetting(){
        Map result = systemClient.getUsefulMailSetting().getResult();
        return result;
    }
    public Map getUsefulSmsSetting(){
        Map result = systemClient.getUsefulSmsSetting().getResult();
        return result;
    }

    public Map getUserById(Long id){
        Map result = systemClient.getUserById(id).getResult();
        return result;
    }

    public Map getUserByLoginName(String loginName){
        List result = systemClient.getUserByLoginName(loginName).getResult();
        if(result.size()>0){
            return (Map)result.get(0);
        }
        return new HashMap();
    }

    public Map getRolesByUser(Long id){
        List result = systemClient.getRolesByUser(id).getResult();
        if(result.size()>0){
            return (Map)result.get(0);
        }
        return new HashMap();
    }
}
